<template>
	<xn-form-container
		:title="formData.id ? '编辑投诉记录' : '增加投诉记录'"
		:width="700"
		v-model:open="open"
		:destroy-on-close="true"
		@close="onClose"
	>
		<a-form ref="complainFormRef" :model="formData" :rules="formRules" layout="vertical">
			<a-form-item label="手机号：" name="mobile">
				<a-input v-model:value="formData.mobile" :disabled="isdisable" placeholder="请输入投诉用户手机号" allow-clear />
			</a-form-item>
			<a-form-item label="身份证号：" name="idCard">
				<a-input v-model:value="formData.idCard" :disabled="isdisable" placeholder="请输入身份证号码" allow-clear />
			</a-form-item>
			<a-form-item label="用户姓名：" name="userName">
				<a-input v-model:value="formData.userName" placeholder="请输入用户姓名" allow-clear />
			</a-form-item>
			<a-form-item label="重要程度：" name="complainImportant">
				<a-select v-model:value="formData.complainImportant" placeholder="请选择投诉重要程度">
					<a-select-option v-for="item in complainImportant" :key="item.type" :value="item.type"
						>{{ item.label }}
					</a-select-option>
				</a-select>
			</a-form-item>
			<a-form-item label="投诉金额：" name="complainAmount">
				<a-input v-model:value="formData.complainAmount" placeholder="请输入投诉金额" allow-clear />
			</a-form-item>
			<a-form-item label="逾期时间：" name="overdueTime">
				<a-date-picker v-model:value="formData.overdueTime" value-format="YYYY-MM-DD" placeholder="逾期时间" />
			</a-form-item>
			<!--			<CustomSelect :type="customSelectProjectType" @update:data="handleUpdateProject" :dataForm="dataForm" />-->
			<a-form-item label="所属项目" name="projectCode">
				<ProjectSelect v-model:value="formData.projectCode" />
			</a-form-item>
			<a-form-item label="投诉内容：" name="complainContent">
				<!-- <a-input
					v-model:value="formData.complainContent"
					style="width: 500px"
					placeholder="请输入投诉内容"
					allow-clear
				/> -->
				<a-textarea v-model:value="formData.complainContent" placeholder="请输入投诉内容" allow-clear />
			</a-form-item>
		</a-form>
		<template #footer>
			<a-button style="margin-right: 8px" @click="onClose">关闭</a-button>
			<a-button type="primary" @click="onSubmit" :loading="submitLoading">保存</a-button>
		</template>
	</xn-form-container>
</template>

<script setup name="complaintForm">
	import { cloneDeep } from 'lodash-es'
	import { required } from '@/utils/formRules'
	import complaintApi from '@/api/tjzy/complaint/complaintApi'
	import ProjectSelect from '@/components/Tjzy/Project/index.vue'
	import { useRoute } from 'vue-router' // 添加这行导入
	const route = useRoute()

	// 抽屉状态
	const open = ref(false)
	const emit = defineEmits({ successful: null })
	const complainFormRef = ref()
	const isdisable = ref(false)
	// 表单数据
	const formData = ref({
		id: undefined,
		mobile: '',
		userName: '',
		complainImportant: '',
		complainAmount: '',
		overdueTime: '',
		idCard: '',
		complainContent: '',
		platformProject: ''
	})
	const submitLoading = ref(false)
	// 打开抽屉
	const onOpen = (record) => {
		open.value = true
		if (record) {
			let recordData = cloneDeep(record)
			formData.value = Object.assign({}, recordData)
			isdisable.value = recordData.id ? true : false
		}
	}
	// 关闭抽屉
	const onClose = () => {
		complainFormRef.value.resetFields()
		formData.value = {}
		open.value = false
		isdisable.value = false
	}
	// 默认要校验的
	const formRules = {
		mobile: [required('')],
		userName: [required('')],
		complainImportant: [required('')],
		complainAmount: [required('')],
		overdueTime: [required('')],
		idCard: [required('')],
		complainContent: [required('')]
	}
	const complainImportant = ref([
		{ type: '一般', label: '一般' },
		{ type: '重要', label: '重要' },
		{ type: '紧急', label: '紧急' }
	])

	// 验证并提交数据
	// const onSubmit = async () => {
	// 	const submittedFormData = cloneDeep(formData.value)
	// 	// 提交表单数据到服务器
	// 	await complaintApi.complaintSubmitForm(submittedFormData, false)
	// }

	// 验证并提交数据
	const onSubmit = () => {
		complainFormRef.value
			.validate()
			.then(() => {
				submitLoading.value = true
				const formDataParam = cloneDeep(formData.value)
				complaintApi
					.complaintSubmitForm(formDataParam, route.name === 'caseDetail' ? false : formDataParam.id)
					.then(() => {
						onClose()
						emit('successful')
					})
					.finally(() => {
						submitLoading.value = false
					})
			})
			.catch(() => {})
	}
	// 抛出函数
	defineExpose({
		onOpen
	})
</script>
